/*
 * Copyright 1999-2020 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sunday.common.sentinel.study.e8_Apacher_Httpclient;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhaoyuguang
 */
@Slf4j
@SpringBootApplication
public class ApacheHttpClientDemoApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(ApacheHttpClientDemoApplication.class);
        initFlowQpsRule();
    }

    @Override
    public void run(String... args) {
    }

    private static void initFlowQpsRule() {
        List<FlowRule> rules = new ArrayList<FlowRule>();
        FlowRule rule1 = new FlowRule();
        rule1.setResource("httpclient:GET:/httpclient/back/{id}");
        // set limit qps to 20
        rule1.setCount(1);
        rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule1.setLimitApp("default");
        rules.add(rule1);
//        FlowRule rule2 = new FlowRule();
//        rule2.setResource("httpclient:GET:/httpclient/back");
//        // set limit qps to 20
//        rule2.setCount(2);
//        rule2.setGrade(RuleConstant.FLOW_GRADE_QPS);
//        rule2.setLimitApp("default");
//        rules.add(rule2);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        log.info("{}", gson.toJson(rules));
        FlowRuleManager.loadRules(rules);
    }
}
